package com.cctv.custom.hibernate.integrated.wrapper;

import org.testng.annotations.Test;

public class BookServiceTest2 extends BasicTest {

	@Test
	public void testGetBooks() {
		BookService bookService = context.getBean(BookService.class);
		Book book = new Book();
		Book book2 = new Book();
		Book book3 = new Book();
		Book book4 = new Book();

		book.setName("Java");
		book2.setName("Java");
		book3.setName("ASP.NET");
		book4.setName("C++");

		book.setAuthor("Ice");
		book2.setAuthor("apache");
		book3.setAuthor("apache");
		book4.setAuthor("bear");

		book.setPrice(10.0);
		book2.setPrice(8.0);
		book3.setPrice(15.0);
		book4.setPrice(15.0);

		bookService.saveOrUpdate(book);
		bookService.saveOrUpdate(book2);
		bookService.saveOrUpdate(book3);
		bookService.saveOrUpdate(book4);

		assert bookService.list("Java", "apache", null).size() == 1;
		assert bookService.list("Java", "apache", 10.0).size() == 0;

		assert bookService.list("ASP.NET", "apache", 15.0).size() == 1;
	}

	@Test
	public void testGetBooksByPrice() {
		BookService bookService = context.getBean(BookService.class);
		Book book = new Book();
		Book book2 = new Book();
		Book book3 = new Book();
		Book book4 = new Book();

		book.setName("Java");
		book2.setName("Java");
		book3.setName("ASP.NET");
		book4.setName("C++");

		book.setAuthor("Ice");
		book2.setAuthor("apache");
		book3.setAuthor("apache");
		book4.setAuthor("bear");

		book.setPrice(10.0);
		book2.setPrice(8.0);
		book3.setPrice(15.0);
		book4.setPrice(15.0);

		bookService.saveOrUpdate(book);
		bookService.saveOrUpdate(book2);
		bookService.saveOrUpdate(book3);
		bookService.saveOrUpdate(book4);

		assert bookService.list(null, null, null).size() == 4;
		assert bookService.list(null, null, 15.0).size() == 2 : bookService
				.list(null, null, 15.0).size();
		assert bookService.list(null, null, 15.0).get(0).getPrice() == 15.0;
		assert bookService.list(null, null, 15.0).get(1).getPrice() == 15.0;

		assert bookService.list(null, null, 8.0).size() == 1;
		assert bookService.list(null, null, 10.0).size() == 1;

		assert bookService.list(null, null, 1.0).size() == 0;
	}

	@Test
	public void testGetBooksByAuthor() {
		BookService bookService = context.getBean(BookService.class);
		Book book = new Book();
		Book book2 = new Book();
		Book book3 = new Book();
		Book book4 = new Book();

		book.setName("Java");
		book2.setName("Java");
		book3.setName("ASP.NET");
		book4.setName("C++");

		book.setAuthor("Ice");
		book2.setAuthor("apache");
		book3.setAuthor("apache");
		book4.setAuthor("bear");

		book.setPrice(10.0);
		book2.setPrice(8.0);
		book3.setPrice(15.0);
		book4.setPrice(15.0);

		bookService.saveOrUpdate(book);
		bookService.saveOrUpdate(book2);
		bookService.saveOrUpdate(book3);
		bookService.saveOrUpdate(book4);

		assert bookService.list(null, null, null).size() == 4;
		assert bookService.list(null, "apache", null).size() == 2;
		assert bookService.list(null, "apache", null).get(0).getAuthor()
				.equals("apache");
		assert bookService.list(null, "apache", null).get(1).getAuthor()
				.equals("apache");

		assert bookService.list(null, "Ice", null).get(0).getAuthor()
				.equals("Ice");
		assert bookService.list(null, "bear", null).get(0).getAuthor()
				.equals("bear");

		assert bookService.list(null, "abc", null).size() == 0;

	}

	@Test
	public void testGetBooksByName() {
		BookService bookService = context.getBean(BookService.class);
		Book book = new Book();
		Book book2 = new Book();
		Book book3 = new Book();
		Book book4 = new Book();

		book.setName("Java");
		book2.setName("Java");
		book3.setName("ASP.NET");
		book4.setName("C++");

		book.setAuthor("Ice");
		book2.setAuthor("apache");
		book3.setAuthor("apache");
		book4.setAuthor("bear");

		book.setPrice(10.0);
		book2.setPrice(8.0);
		book3.setPrice(15.0);
		book4.setPrice(15.0);

		bookService.saveOrUpdate(book);
		bookService.saveOrUpdate(book2);
		bookService.saveOrUpdate(book3);
		bookService.saveOrUpdate(book4);

		assert bookService.list(null, null, null).size() == 4;
		assert bookService.list("Java", null, null).size() == 2;
		assert bookService.list("Java", null, null).get(0).getName()
				.equals("Java");
		assert bookService.list("Java", null, null).get(1).getName()
				.equals("Java");

		assert bookService.list("ASP.NET", null, null).size() == 1;
		assert bookService.list("C++", null, null).size() == 1;

		assert bookService.list("aaaa", null, null).size() == 0;
	}
}
